package org.laowenchao.client;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.laowenchao.client.model.Track;
import org.laowenchao.client.rpc.PositionService;
import org.laowenchao.client.rpc.PositionServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;  
import com.extjs.gxt.ui.client.event.Events;  
import com.extjs.gxt.ui.client.event.Listener;  
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Params;  
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.DatePicker;  
import com.extjs.gxt.ui.client.widget.Info; 
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;  
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.RootPanel;


public class MyTrack implements EntryPoint {

	PositionServiceAsync positionService =GWT.create(PositionService.class);
	ListStore<Track> store = new ListStore<Track>(); 

	@Override
	public void onModuleLoad() {

		((ServiceDefTarget)positionService).setServiceEntryPoint("/PositionService");

		final DatePicker picker = new DatePicker(); 		 
		picker.addListener(Events.Select, new Listener<ComponentEvent>() {  
			
			public void handleEvent(ComponentEvent be) {  
				picker.hide();
				Date selectedDate = picker.getValue();
				String d = DateTimeFormat.getShortDateFormat().format(selectedDate);  
				Info.display("Date Selected", "You selected {0}.", new Params(d));  
				refreshTrack(selectedDate);
			}  

		});		


		
		RootPanel.get("DatePicker").add(picker); 
		
		picker.setVisible(false);
		
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
	    
	    ColumnConfig column = new ColumnConfig("date","时间",150);  
	    configs.add(column);  
	  
	    column = new ColumnConfig("lat","纬度",100);  
	    configs.add(column);  
	  
	    column = new ColumnConfig("lon","经度",100);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("location");
	    column.setHeader("位置");	 
	    column.setWidth(450);
	    column.setAlignment(HorizontalAlignment.LEFT);  
	    configs.add(column);  

//	    CheckColumnConfig checkColumn = new CheckColumnConfig("select", "Select?", 55);  
//	    CellEditor checkBoxEditor = new CellEditor(new CheckBox());  
//	    checkColumn.setEditor(checkBoxEditor);  
//	    configs.add(checkColumn);  
		
	    ColumnModel cm = new ColumnModel(configs);  
	
	    
	    Date now = new Date();
	    now.setHours(0);
	    now.setMinutes(0);
	    now.setSeconds(0);
	    
	    refreshTrack(now);
   
	    Grid<Track> grid = new Grid<Track>(store, cm);  
	   	    
	    ContentPanel cp = new ContentPanel();  
	    cp.setBodyBorder(false);  
	    cp.setHeading("My Track");  
	    cp.setButtonAlign(HorizontalAlignment.CENTER);  
	    cp.setLayout(new FitLayout());  
	    cp.setSize(800, 600);  
	    
	    grid.setStyleAttribute("borderTop", "none");  
	    grid.setAutoExpandColumn("date");  
	    grid.setBorders(true);  
	    grid.setStripeRows(true);  
	    cp.add(grid);  
	    
	    cp.addButton(new Button("Pick Date", new SelectionListener<ButtonEvent>() {  

	    	@Override  
	    	public void componentSelected(ButtonEvent ce) { 
	    		
	    		picker.show();
	    	}  
	    }));  
	    
	    RootPanel.get("TrackList").add(cp);
	}
	
	private void refreshTrack(Date selectedDate)
	{
		System.out.println(selectedDate);
		positionService.getTrackByDate2(selectedDate, new AsyncCallback<List<Track>>(){
		
			
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}


			@Override
			public void onSuccess(List<Track> result) {
				// TODO Auto-generated method stub
				store.removeAll();
				store.add(result);
			}
			
		});
	}

}
